package com.eastfair.venueservice.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.EvaluateComplaintDTO;
import com.eastfair.venueservice.dto.EvaluateComplaintPageQuery;
import com.eastfair.venueservice.dto.EvaluateComplaintSaveDTO;
import com.eastfair.venueservice.dto.EvaluateComplaintUpdateDTO;
import com.eastfair.venueservice.entity.EvaluateComplaint;
import com.eastfair.venueservice.service.EvaluateComplaintService;
import com.eastfair.venueservice.vo.EvaluateComplaintVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 评价投诉记录
 * </p>
 *
 * @author lyw
 * @date 2023-02-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/evaluateComplaint")
@Api(value = "EvaluateComplaint", tags = "评价投诉记录")
@PreAuth(replace = "venueservice:evaluateComplaint:")
public class EvaluateComplaintController extends SuperController<EvaluateComplaintService, Long, EvaluateComplaint, EvaluateComplaintPageQuery, EvaluateComplaintSaveDTO, EvaluateComplaintUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private EvaluateComplaintService evaluateComplaintService;

    @Override
    public void handlerResult(IPage<EvaluateComplaint> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<EvaluateComplaint> evaluateComplaintList = list.stream().map((map) -> {
            EvaluateComplaint evaluateComplaint = EvaluateComplaint.builder().build();
            //TODO 请在这里完成转换
            return evaluateComplaint;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(evaluateComplaintList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<EvaluateComplaintPageQuery> params) {
        EvaluateComplaintPageQuery pageQuery = params.getModel();
    }

    /**
     * 分页查询评价投诉列表
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/page/evaluateComplaintList")
    public R<IPage<EvaluateComplaintVO>> queryEvaluateComplaintList(@RequestBody PageParams<EvaluateComplaintDTO> pageParams) {
        log.info("queryEvaluateComplaint - 分页查询评价投诉列表, pageParams={}", pageParams);
        try {
            return R.successDef(evaluateComplaintService.queryEvaluateComplaintList(pageParams));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 场馆综合展会详情查询评价投诉列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @PostMapping("/query/for/venue")
    public R<IPage<EvaluateComplaintVO>> queryForVenueExhibition(@RequestBody PageParams<EvaluateComplaintDTO> pageParams) {
        log.info("queryForVenueExhibition - 场馆综合展会详情查询评价投诉列表, pageParams={}", pageParams);
        return R.successDef(evaluateComplaintService.queryForVenueExhibition(pageParams));
    }

    /**
     * 保存评价投诉
     * @param evaluateComplaintDTO
     * @return
     */
    @ApiOperation(value = "保存评价投诉")
    @SysLog("添加评价投诉")
    @PostMapping(value = "/saveEvaluateComplaintInfo")
    public R<Boolean> saveEvaluateComplaintInfo(@RequestBody EvaluateComplaintDTO evaluateComplaintDTO, HttpServletRequest request){
        try {
            String ip = request.getHeader("X-Requested-For");
            if (StringUtil.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            evaluateComplaintDTO.setIpAddress(ip);
            return R.success(evaluateComplaintService.saveEvaluateComplaintInfo(evaluateComplaintDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }


    /**
     * 修改评价投诉
     * @param evaluateComplaintDTO
     * @return
     */
    @ApiOperation(value = "修改评价投诉")
    @SysLog("修改评价投诉")
    @PostMapping(value = "/updateEvaluateComplaintById")
    public R<Boolean> updateEvaluateComplaintById(@RequestBody EvaluateComplaintDTO evaluateComplaintDTO){
        try {
            return R.success(evaluateComplaintService.updateEvaluateComplaintById(evaluateComplaintDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * 更新评价回复.
     *
     * @param evaluateComplaintDTO the evaluate complaint dto
     * @return the r
     */
    @SysLog("评价投诉回复")
    @PostMapping(value = "/update/reply")
    public R<Boolean> updateReply(@RequestBody EvaluateComplaintDTO evaluateComplaintDTO) {
        log.info("updateReply - 更新评价回复, evaluateComplaintDTO={}", evaluateComplaintDTO);
        return R.success(evaluateComplaintService.updateEvaluateComplaintReply(evaluateComplaintDTO));
    }

    /**
     * 删除评价投诉
     * @param evaluateComplaintDTO
     * @return
     */
    @ApiOperation(value = "删除评价投诉")
    @SysLog("删除评价投诉")
    @PostMapping(value = "/delEvaluateComplaintById")
    public R<Boolean> delEvaluateComplaintById(@RequestBody EvaluateComplaintDTO evaluateComplaintDTO){
        try {
            return R.success(evaluateComplaintService.delEvaluateComplaintById(evaluateComplaintDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }

    }

    /**
     * id查询评价投诉
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/getEvaluateComplaintInfoById/{id}")
    public R<EvaluateComplaintVO> getEvaluateComplaintInfoById(@PathVariable("id") Long id) {
        log.info("getEvaluateComplaintInfoById - id查询评价投诉, id={}", id);
        try {
            return R.successDef(evaluateComplaintService.getEvaluateComplaintInfoById(id));
        } catch (BizException e) {
            return R.fail(e);
        }
    }


    /**
     * 服务商id查询服务商信誉等级
     *
     * @param id the id
     * @return the by id
     */
    @GetMapping("/getReputationLevelByServiceProviderId/{id}")
    public R<Map<String,List<String>>> getReputationLevelById(@PathVariable("id") Long id) {
        log.info("getReputationLevelById - 服务商id查询服务商信誉等级, id={}", id);
        try {
            return R.successDef(evaluateComplaintService.getReputationLevelById(id));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 展会id的统计评星数量.
     *
     * @param exhibitionManageId the exhibition manage id
     * @return the r
     */
    @GetMapping("/count/evaluate/score/{exhibitionManageId}")
    public R<List<Map<String, Object>>> countScoreByExhibitionId(@PathVariable("exhibitionManageId") Long exhibitionManageId) {
        log.info("countEvaluateScoreByExhibitionId - 展会id的统计评星数量, exhibitionManageId={}", exhibitionManageId);
        return R.successDef(evaluateComplaintService.countScoreByExhibitionId(exhibitionManageId));
    }

    /**
     * 展会id的统计评价数量和未评价数量.
     *
     * @param exhibitionManageId the exhibition manage id
     * @return the r
     */
    @GetMapping("/count/evaluate/service/{exhibitionManageId}")
    public R<List<Map<String, Object>>> countEvaluateServiceByExhibitionId(@PathVariable("exhibitionManageId") Long exhibitionManageId) {
        log.info("countEvaluateServiceItemByExhibitionId - 展会id的统计评价数量和未评价数量, exhibitionManageId={}", exhibitionManageId);
        return R.successDef(evaluateComplaintService.countEvaluateServiceByExhibitionId(exhibitionManageId));
    }

    /**
     * 展会ID统计各个服务的星级.
     *
     * @param exhibitionManageId the exhibition manage id
     * @param topN               the top n
     * @return the r
     */
    @GetMapping("/count/service/score")
    public R<Map<String, Object>> countServiceScoreByExhibitionId(@RequestParam("exhibitionManageId") Long exhibitionManageId,
                                                                  @RequestParam("topN") Integer topN) {
        log.info("countServiceScoreByExhibitionId - 展会ID统计各个服务的星级, exhibitionManageId={}, topN={}", exhibitionManageId, topN);
        return R.successDef(evaluateComplaintService.countServiceScoreByExhibitionId(exhibitionManageId, topN));
    }

    /**
     * 查询主场服务评价数量.
     *
     * @param exhibitionManageId the exhibition manage id
     * @param homeId             the home id
     * @return the r
     */
    @GetMapping("/count/home/service/evaluate")
    public R<Map<String, Object>> countHomeServiceEvaluate(@RequestParam("exhibitionManageId") Long exhibitionManageId,
                                                           @RequestParam("homeId") Long homeId) {
        log.info("countHomeServiceEvaluate - 查询主场服务评价数量, exhibitionManageId={}, homeId={}", exhibitionManageId, homeId);
        return R.successDef(evaluateComplaintService.countHomeServiceEvaluate(exhibitionManageId, homeId));
    }
}
